<template>
  <div class="xtx-form">
    <div class="user-info">
      <img :src="avatar" alt="" />
      <p>Hi，{{nickname}} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~</p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input class="input" type="text" v-model="form.mobile" placeholder="绑定的手机号" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input class="input" type="text" placeholder="短信验证码" v-model="form.code" />
        <span class="code" @click="send">{{time === 0 ? '发送验证码' : time+ '秒后重试'}}</span>
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" class="submit" @click="submit">立即绑定</a>
  </div>
</template>

<script>
import QC from 'qc'
import { reactive, ref } from 'vue'
import { useCountDown } from '@/compositions'
import { message } from '@/components/Message'
import { reqUserQQBindCode, reqUserQQBindLogin } from '@/api/user'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
export default {
  name: 'CallbackBind',
  props: {
    openId: {
      type: String,
      default: ''
    }
  },
  setup (props) {
    // 设置头像
    const avatar = ref('')
    const nickname = ref('')
    // QC开头的方法都是QC第三方组件提供的 QC.Login.check() 验证是否登录成功
    if (QC.Login.check()) {
      // QC.api('get_user_info')  获取qq基本信息
      QC.api('get_user_info').success((res) => {
        console.log(res)
        avatar.value = res.data.figureurl_qq_2
        nickname.value = res.data.nickname
      })
    }
    const { time, start } = useCountDown()
    const form = reactive({
      mobile: '',
      code: ''
    })
    // 验证码
    const send = async () => {
      if (time.value === 0) {
        if (!/^1[3-9]\d{9}$/.test(form.mobile)) {
          message({
            type: 'error',
            text: '请输入正确手机号'
          })
          return false
        }
        const res = await reqUserQQBindCode(form.mobile)
        message({
          type: 'success',
          text: '验证码发送成功'
        })
        // 开启倒计时
        start()
        console.log('我要发送验证码了')
      }
    }

    // 发送请求进行绑定
    const store = useStore()
    const router = useRouter()
    const submit = async () => {
      // 校验一下手机号和验证码
      if (form.mobile !== '' && form.code !== '') {
        const res = await reqUserQQBindLogin({
          unionId: props.openId,
          ...form
        })
        // 1. 提示
        message({
          type: 'success',
          text: '绑定成功'
        })
        console.log('res++++++', res)
        // 2. 调用mutation, 将个人信息,存到vuex中
        store.commit('user/setProfile', res.result)
        // 3. 跳转到首页
        router.push('/')
      }
    }
    return {
      avatar,
      nickname,
      time,
      form,
      send,
      submit
    }
  }
}
</script>

<style scoped lang='less'>
.user-info {
    width: 320px;
    height: 70px;
    margin: 0 auto;
    display: flex;
    background: #f2f2f2;
    align-items: center;
    padding: 0 10px;
    margin-bottom: 25px;
    img {
      background: #f2f2f2;
      width: 50px;
      height: 50px;
    }
    p {
      padding-left: 10px;
    }
  }
  .code {
    position: absolute;
    right: 0;
    top: 0;
    line-height: 50px;
    width: 80px;
    color: #999;
    &:hover {
      cursor: pointer;
    }
  }
</style>
